package com.jscm.showdata;

import com.jscm.openprocess.OpenProcessListView;
import com.jscm.openselect.SelecthBoxSearchBtn;
import com.jscm.openselect.SelecthBoxSearchBtnListAddress;
import com.jscm.openselect.selectShowWindow;
import com.jscm.showAttribute.AttributeTextArea;
import com.jscm.showHexBool.HexBoolTextArea;
import com.jscm.showHexBool.HexBoolWindow;
import com.scm.all.export.ByteUtils;
import com.scm.all.pfunc.ByteIndexCallBack;
import com.scm.all.struct.TagPmemory_Basic_InfoRmationX64;
import com.scm.x64.export.MemoryOperationUtilsX64;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;

import java.util.Arrays;

/**
 * 数据视图列表
 */
public class SelectDataView {
    private static TableColumn<TagMomeory,String> tableColumn1 = new TableColumn<>("静态");
    private static TableColumn<TagMomeory,String> tableColumn2 = new TableColumn<>("地址");
    private static TableColumn<TagMomeory,String> tableColumn3 = new TableColumn<>("数据");
    private static TableColumn<TagMomeory,String> tableColumn4 = new TableColumn<>("区域地址");
    private static TableColumn<TagMomeory,String> tableColumn5 = new TableColumn<>("搜索类型");

    private static TableColumn<TagMomeory,String> tableColumn6 = new TableColumn<>("长度");
    public static ObservableList<TagMomeory> observableList = FXCollections.observableArrayList();
    public static TableView<TagMomeory> tableView = new TableView<TagMomeory>(observableList);
    public static TableView.TableViewSelectionModel<TagMomeory> defaultSelectionModel = tableView.getSelectionModel();
    static {
        init();
    }

    private static void init() {
        tableView.setLayoutX(0);
        tableView.setLayoutY(50);
        tableView.setPrefWidth(483);
        tableView.setPrefHeight(400);
        tableColumn1.setPrefWidth(40);
        tableColumn2.setPrefWidth(70);
        tableColumn3.setPrefWidth(280);
        tableColumn4.setPrefWidth(70);
        tableColumn5.setPrefWidth(70);

        tableColumn1.setSortable(false);
        tableColumn2.setSortable(false);
        tableColumn3.setSortable(false);
        tableColumn4.setSortable(false);
        tableColumn5.setSortable(false);
        tableColumn6.setSortable(false);
        SelectDataView.tableView.setTableMenuButtonVisible(false);
        tableView.setPlaceholder(new Label("暂未加载任何数据"));
        tableColumn1.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TagMomeory, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<TagMomeory, String> param) {
                SelectDataView.tableView.getSelectionModel().clearSelection();
                if(SelecthBoxSearchBtn.flag){
                    selectShowWindow.stage.setTitle("条件搜索 搜索结果：" + SelectDataView.observableList.size() +" 搜索完毕");
                }else {
                    selectShowWindow.stage.setTitle("条件搜索 搜索结果：" + SelectDataView.observableList.size());
                }

                //滚动条置底
//                SelectDataView.tableView.scrollTo(SelectDataView.observableList.size());
                SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().getIsStatic());
                return simpleStringProperty;
            }
        });
        tableColumn2.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TagMomeory, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<TagMomeory, String> param) {
                SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().getHexData());
                return simpleStringProperty;
            }
        });
        tableColumn3.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TagMomeory, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<TagMomeory, String> param) {
                SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().getUpdateValue());
                return simpleStringProperty;
            }
        });
        tableColumn4.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TagMomeory, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<TagMomeory, String> param) {
                SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().getBaseAddress());
                return simpleStringProperty;
            }
        });
        tableColumn5.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TagMomeory, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<TagMomeory, String> param) {
                SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().getTypeData());
                return simpleStringProperty;
            }
        });
        tableColumn6.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TagMomeory, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<TagMomeory, String> param) {
                SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().getLen());

                return simpleStringProperty;
            }
        });
        tableView.getColumns().addAll(tableColumn1,tableColumn2,tableColumn6,tableColumn4,tableColumn5,tableColumn3);
        tableView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                event.consume();
//                SelecthBoxSearchBtnListAddress.flag=false;
//                SelecthBoxSearchBtnListAddress.button.setText("开始列表刷新");
            }
        });
        tableView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
//            SelecthBoxSearchBtnListAddress.flag=false;
//            SelecthBoxSearchBtnListAddress.button.setText("开始列表刷新");
            if(newValue!=null){
                String data = "";
                data+="内存地址HEX："+newValue.getHexData().toUpperCase()+"   (十进制："+ByteUtils.hexToLong1(newValue.getHexData().toUpperCase())+")"+"\n";
                TagPmemory_Basic_InfoRmationX64 tagPmemory_basic_infoRmationX64 = MemoryOperationUtilsX64.selectMemoryAddressAttribute(OpenProcessListView.processHandler, ByteUtils.hexToLong1(newValue.getHexData()));
                data+="区域地址HEX："+ByteUtils.longToHexString(tagPmemory_basic_infoRmationX64.BaseAddress).toUpperCase()+"   (十进制："+tagPmemory_basic_infoRmationX64.BaseAddress+")"+"\n";
                data+="区域偏移量HEX："+newValue.getBaseAddress().toUpperCase()+"   (十进制："+ByteUtils.hexToLong1(newValue.getBaseAddress().toUpperCase())+")"+"\n";
                data+="分配基地址HEX："+ByteUtils.longToHexString(tagPmemory_basic_infoRmationX64.AllocationBase).toUpperCase()+"   (十进制："+tagPmemory_basic_infoRmationX64.AllocationBase+")"+"\n";
                data+="分配偏移量HEX："+ByteUtils.longToHexString(ByteUtils.hexToLong1(newValue.getHexData())-tagPmemory_basic_infoRmationX64.AllocationBase).toUpperCase()+"   ("+(ByteUtils.hexToLong1(newValue.getHexData())-tagPmemory_basic_infoRmationX64.AllocationBase)+")"+"\n";
                data+="保护："+ByteUtils.longToHexString(tagPmemory_basic_infoRmationX64.Protect)+"   (十进制："+tagPmemory_basic_infoRmationX64.Protect+")"+"\n";
                data+="原保护："+ByteUtils.longToHexString(tagPmemory_basic_infoRmationX64.AllocationProtect)+"   (十进制："+tagPmemory_basic_infoRmationX64.AllocationProtect+")"+"\n";
                data+="内存大小："+ByteUtils.longToHexString(tagPmemory_basic_infoRmationX64.RegionSize)+"   (十进制："+tagPmemory_basic_infoRmationX64.RegionSize+")"+"\n";
                data+="内存类型："+ByteUtils.longToHexString(tagPmemory_basic_infoRmationX64.lType)+"   (十进制："+tagPmemory_basic_infoRmationX64.lType+")"+"\n";
                byte[] dat =MemoryOperationUtilsX64.readProcessMemoryByte(OpenProcessListView.processHandler, ByteUtils.hexToLong1(newValue.getHexData()),Integer.parseInt(newValue.getLen()));
                data+="内存数据："+ByteUtils.bytesToHexString(dat).toUpperCase()+"   (十进制："+ Arrays.toString(dat) +")"+"\n";
                AttributeTextArea.textArea.setText(data);
                HexBoolWindow.stage.setTitle("特征分析工具[ 当前地址："+newValue.getHexData().toUpperCase()+"   当前长度："+tagPmemory_basic_infoRmationX64.RegionSize+" ] ");
//                将字节数据添加到特征分析字段上
//                byte[] datAll = MemoryOperationUtilsX64.readProcessMemoryByte(OpenProcessListView.processHandler, ByteUtils.hexToLong1(newValue.getHexData()),tagPmemory_basic_infoRmationX64.RegionSize);
//                HexBoolTextArea.textArea.setText(Arrays.toString(datAll));
            }
        });
    }
}
